#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <libavutil/avassert.h>
#include <libavutil/channel_layout.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libswresample/swresample.h>

#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <poll.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
#include <signal.h>
#include <pthread.h>

//推流的服务器地址
#define RTMP_SERVE_RADDR  "rtmp://192.168.137.174/live/test"

#define STREAM_DURATION   30.0   /*录制10秒的视频，由于缓冲的原因，一般只有8秒*/
#define STREAM_FRAME_RATE 15     /* 15 images/s   avfilter_get_by_name */
#define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
#define SCALE_FLAGS SWS_BICUBIC


/*一些摄像头需要使用的全局变量*/
unsigned char *image_buffer[4];
int video_fd;
pthread_mutex_t mutex;
pthread_cond_t cond;

/*一些audio需要使用的全局变量*/
pthread_mutex_t mutex_audio;

extern int capture_audio_data_init( char *audio_dev);
extern int capture_audio_data(snd_pcm_t *capture_handle,int buffer_frames);
/*
 进行音频采集，采集pcm数据并直接保存pcm数据
 音频参数： 
	 声道数：		2
	 采样位数：	16bit、LE格式
	 采样频率：	44100Hz
*/
#define AudioFormat SND_PCM_FORMAT_S16_LE  //指定音频的格式,其他常用格式：SND_PCM_FORMAT_U24_LE、SND_PCM_FORMAT_U32_LE
#define AUDIO_CHANNEL_SET   1  			  //1单声道   2立体声
#define AUDIO_RATE_SET 16000   //音频采样率,常用的采样频率: 44100Hz 、16000HZ、8000HZ、48000HZ、22050HZ
FILE *pcm_data_file=NULL;

int buffer_frames;
snd_pcm_t *capture_handle;
snd_pcm_format_t format=AudioFormat;


//保存音频数据链表
struct AUDIO_DATA
{
	unsigned char* audio_buffer;
	struct AUDIO_DATA *next;
};

//定义一个链表头
struct AUDIO_DATA *list_head=NULL;
struct AUDIO_DATA *List_CreateHead(struct AUDIO_DATA *head);
void List_AddNode(struct AUDIO_DATA *head,unsigned char* audio_buffer);
void List_DelNode(struct AUDIO_DATA *head,unsigned char* audio_buffer);
int List_GetNodeCnt(struct AUDIO_DATA *head);

// 单个输出AVStream的包装器
typedef struct OutputStream {
    AVStream *st;
    AVCodecContext *enc;

    /* 下一帧的点数*/
    int64_t next_pts;
    int samples_count;

    AVFrame *frame;
    AVFrame *tmp_frame;

    float t, tincr, tincr2;

    struct SwsContext *sws_ctx;
    struct SwrContext *swr_ctx;
} OutputStream;


static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
{
    /*将输出数据包时间戳值从编解码器重新调整为流时基 */
    av_packet_rescale_ts(pkt, *time_base, st->time_base);
    pkt->stream_index = st->index;
		
	/*将压缩的帧写入媒体文件*/
    return av_interleaved_write_frame(fmt_ctx, pkt);
}


/* 添加输出流。 */
static void add_stream(OutputStream *ost, AVFormatContext *oc,
                       AVCodec **codec,
                       enum AVCodecID codec_id)
{
    AVCodecContext *c;
    int i;

    /* find the encoder */
    *codec = avcodec_find_encoder(codec_id);
    if (!(*codec)) {
        fprintf(stderr, "Could not find encoder for '%s'\n",
                avcodec_get_name(codec_id));
        exit(1);
    }

    ost->st = avformat_new_stream(oc, NULL);
    if (!ost->st) {
        fprintf(stderr, "Could not allocate stream\n");
        exit(1);
    }
    ost->st->id = oc->nb_streams-1;
    c = avcodec_alloc_context3(*codec);
    if (!c) {
        fprintf(stderr, "Could not alloc an encoding context\n");
        exit(1);
    }
    ost->enc = c;

    switch ((*codec)->type) {
    case AVMEDIA_TYPE_AUDIO:
        c->sample_fmt  = (*codec)->sample_fmts ? (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
        c->bit_rate    = 64000;  //设置码率
        c->sample_rate = 16000;  //音频采样率
        c->channels= av_get_channel_layout_nb_channels(c->channel_layout);
        c->channel_layout = AV_CH_LAYOUT_MONO; 
		//AV_CH_LAYOUT_MONO 单声道   AV_CH_LAYOUT_STEREO 立体声
        c->channels        = av_get_channel_layout_nb_channels(c->channel_layout);
        ost->st->time_base = (AVRational){ 1, c->sample_rate };
        break;

    default:
        break;
    }

    /* 某些格式希望流头分开。 */
    if (oc->oformat->flags & AVFMT_GLOBALHEADER)
        c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}

/**************************************************************/
/* audio output */

static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
                                  uint64_t channel_layout,
                                  int sample_rate, int nb_samples)
{
    AVFrame *frame = av_frame_alloc();
    frame->format = sample_fmt;
    frame->channel_layout = channel_layout;
    frame->sample_rate = sample_rate;
    frame->nb_samples = nb_samples;
    if(nb_samples)
	{
        av_frame_get_buffer(frame, 0);
    }
    return frame;
}

static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
    AVCodecContext *c;
    int nb_samples;
    int ret;
    AVDictionary *opt = NULL;
    c = ost->enc;
    av_dict_copy(&opt, opt_arg, 0);
    ret = avcodec_open2(c, codec, &opt);
    av_dict_free(&opt);
	

    /*下面3行代码是为了生成虚拟的声音设置的频率参数*/
    ost->t     = 0;
    ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
    ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;

	//AAC编码这里就固定为1024
    nb_samples = c->frame_size;

    ost->frame     = alloc_audio_frame(c->sample_fmt, c->channel_layout,
                                       c->sample_rate, nb_samples);
    ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
                                       c->sample_rate, nb_samples);

    /* copy the stream parameters to the muxer */
    avcodec_parameters_from_context(ost->st->codecpar, c);

    /* create resampler context */
    ost->swr_ctx = swr_alloc();

	/* set options */
    printf("c->channels=%d\n",c->channels);
	av_opt_set_int       (ost->swr_ctx, "in_channel_count",   c->channels,       0);
	av_opt_set_int       (ost->swr_ctx, "in_sample_rate",     c->sample_rate,    0);
	av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt",      AV_SAMPLE_FMT_S16, 0);
	av_opt_set_int       (ost->swr_ctx, "out_channel_count",  c->channels,       0);
	av_opt_set_int       (ost->swr_ctx, "out_sample_rate",    c->sample_rate,    0);
	av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt",     c->sample_fmt,     0);
    
	/* initialize the resampling context */
	swr_init(ost->swr_ctx);
}

/* 毫秒级 延时 */
void Sleep(int ms)
{
	struct timeval delay;
	delay.tv_sec = 0;
	delay.tv_usec = ms * 1000; // 20 ms
	select(0, NULL, NULL, NULL, &delay);
}


/*
准备虚拟音频帧
这里可以替换成从声卡读取的PCM数据
*/
static AVFrame *get_audio_frame(OutputStream *ost)
{
    AVFrame *frame = ost->tmp_frame;
    int j, i, v;
    int16_t *q = (int16_t*)frame->data[0];

    // 检查我们是否要生成更多帧，用于判断是否结束
    // if (av_compare_ts(ost->next_pts, ost->enc->time_base, STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
    //     return NULL;

#if 1
    int cnt = 0;
    while (cnt <= 0)
    {
        // usleep(1000);  // 每毫秒检查一次
        cnt = List_GetNodeCnt(list_head);
    }

    pthread_mutex_lock(&mutex_audio); /* 互斥锁上锁 */

    struct AUDIO_DATA *tmp = list_head;
    if (tmp != NULL && tmp->next != NULL) {
        tmp = tmp->next;
    }

    if (tmp == NULL) {
        printf("数据为空。\n");
        pthread_mutex_unlock(&mutex_audio);
        return NULL;  // 结束当前帧的处理
    }

    unsigned char *buffer = tmp->audio_buffer;
    memcpy(q, buffer, frame->nb_samples * sizeof(int16_t) * ost->enc->channels); // 拷贝数据到frame缓冲区

    List_DelNode(list_head, buffer);
    free(buffer);

    pthread_mutex_unlock(&mutex_audio); /* 互斥锁解锁 */
#endif

    frame->pts = ost->next_pts;
    ost->next_pts += frame->nb_samples;
    return frame;
}




static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
{
    AVCodecContext *c;
    AVPacket pkt = { 0 };
    AVFrame *frame;
    int ret;
    int got_packet;
    int dst_nb_samples;

    av_init_packet(&pkt);
    c = ost->enc;

    frame = get_audio_frame(ost);

    if(frame)
	{
        /*使用重采样器将样本从本机格式转换为目标编解码器格式*/
		 /*计算样本的目标数量*/
		dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
										c->sample_rate, c->sample_rate, AV_ROUND_UP);
		av_assert0(dst_nb_samples == frame->nb_samples);
        av_frame_make_writable(ost->frame);
        /*转换为目标格式 */
        swr_convert(ost->swr_ctx,
                    ost->frame->data, dst_nb_samples,
                    (const uint8_t **)frame->data, frame->nb_samples);
        frame = ost->frame;
        frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
        ost->samples_count += dst_nb_samples;
    }

    avcodec_encode_audio2(c, &pkt, frame, &got_packet);

    if (got_packet) 
	{
        write_frame(oc, &c->time_base, ost->st, &pkt);
    }
    return (frame || got_packet) ? 0 : 1;
}



static void close_stream(AVFormatContext *oc, OutputStream *ost)
{
    avcodec_free_context(&ost->enc);
    av_frame_free(&ost->frame);
    av_frame_free(&ost->tmp_frame);
    sws_freeContext(ost->sws_ctx);
    swr_free(&ost->swr_ctx);
}


int audio_encode(char *filename) {
    OutputStream video_st = { 0 }, audio_st = { 0 };
    AVOutputFormat *fmt;
    AVFormatContext *oc;
    AVCodec *audio_codec;
    int ret;
    int have_audio = 0;
    int encode_audio = 0;
    AVDictionary *opt = NULL;
    
    /* 分配输出环境 */
    ret = avformat_alloc_output_context2(&oc, NULL, "flv", filename);
    if (ret < 0) {
        fprintf(stderr, "无法分配输出上下文: %s\n", av_err2str(ret));
        return 1;
    }
    fmt = oc->oformat;

    if (!fmt) {
        fprintf(stderr, "输出格式无效\n");
        return 1;
    }

    // 打印格式信息
    printf("输出格式: %s\n", fmt->name);

    // 指定编码器
    fmt->audio_codec = AV_CODEC_ID_AAC;

    /* 使用默认格式的编解码器添加音频流，初始化编解码器 */
    if (fmt->audio_codec != AV_CODEC_ID_NONE) {
        printf("添加音频流\n");
        add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);

        have_audio = 1;
        encode_audio = 1;
    }
    else
    printf("stop\n");

    /* 确保音频编码器正确初始化 */
    if (have_audio) {
        printf("初始化音频编码器\n");
        open_audio(oc, audio_codec, &audio_st, opt);

    }

    av_dump_format(oc, 0, filename, 1); // 打印媒体文件格式信息

    /* 打开输出文件（如果需要） */
    if (!(fmt->flags & AVFMT_NOFILE)) {
        printf("打开输出文件: %s\n", filename);
        ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
        if (ret < 0) {
            fprintf(stderr, "无法打开输出文件: '%s': %s\n", filename, av_err2str(ret));
            return 1;
        }
    }

    /* 编写流头（如果有） */
    ret = avformat_write_header(oc, &opt);
    if (ret < 0) {
        fprintf(stderr, "无法写入流头: %s\n", av_err2str(ret));
        return 1;
    }

    /* 编码循环 */
while (encode_audio)
{

        encode_audio = !write_audio_frame(oc, &audio_st);

}


    av_write_trailer(oc);

    if (have_audio) {
        printf("关闭音频流\n");
        close_stream(oc, &audio_st);
    }

    if (!(fmt->flags & AVFMT_NOFILE)) {
        printf("关闭输出文件\n");
        avio_closep(&oc->pb);
    }
    avformat_free_context(oc);

    return 0;
}



/*
子线程函数: 采集摄像头的数据
*/
void *pthread_read_audio_data(void *arg)
{
    capture_audio_data(capture_handle,buffer_frames);
}

//运行示例:  ./a.out /dev/video0
int main(int argc,char **argv)
{
	if(argc!=2)
	{
		printf("./app </dev/videoX> <hw:X> \n");
		return 0;
	}
	int err;
	pthread_t thread_id;
	
	//创建链表头
	list_head=List_CreateHead(list_head);
	
	/*初始化互斥锁*/
	pthread_mutex_init(&mutex,NULL);
	/*初始化条件变量*/
	pthread_cond_init(&cond,NULL);

    /*初始化互斥锁*/
	pthread_mutex_init(&mutex_audio,NULL);

    capture_audio_data_init( argv[1]);
    /*创建子线程: 采集音频的数据*/
	pthread_create(&thread_id,NULL,pthread_read_audio_data,NULL);
	/*设置线程的分离属性: 采集摄像头的数据*/
	pthread_detach(thread_id);
	
	char filename[100];
	time_t t;
	struct tm *tme;


	//开始音频、视频编码
	while(1)
	{
		//开始视频编码
		audio_encode(RTMP_SERVE_RADDR);
	}
	return 0;
}

/*
函数功能： 创建链表头
*/
struct AUDIO_DATA *List_CreateHead(struct AUDIO_DATA *head)
{
	if(head==NULL)
	{
		head=malloc(sizeof(struct AUDIO_DATA));
		head->next=NULL;
	}
	return head;
}

/*
函数功能: 插入新的节点
*/
void List_AddNode(struct AUDIO_DATA *head, unsigned char* audio_buffer) {
    struct AUDIO_DATA *tmp = head;
    struct AUDIO_DATA *new_node;
    while (tmp->next) {
        tmp = tmp->next;
    }
    new_node = malloc(sizeof(struct AUDIO_DATA));
    new_node->audio_buffer = audio_buffer;
    new_node->next = NULL;
    tmp->next = new_node;

    printf("Added new node: %p\n", new_node); // Debug information
}


/*
函数功能:删除节点
*/
void List_DelNode(struct AUDIO_DATA *head, unsigned char *audio_buffer)
{

    // 删除节点时，正确更新链表头指针
if (head == NULL) {
    printf("链表为空，无法删除节点\n");
    return;
                   }

    struct AUDIO_DATA *tmp = head;
    struct AUDIO_DATA *p;

    // 输出链表删除操作开始的信息
    printf("Starting to delete node with audio_buffer=%p\n", audio_buffer);

    // 如果链表为空，直接返回
    if (tmp == NULL) {
        printf("List is empty, nothing to delete\n");
        return;
    }

    // 如果要删除的是头节点
    if (tmp->audio_buffer == audio_buffer) {
        printf("Found node to delete (head) with audio_buffer=%p\n", audio_buffer);
        head = tmp->next; // 更新头节点
        free(tmp); // 释放节点
        printf("Head node deleted\n");
        return; // 删除后直接返回
    }

    // 找到链表中要删除的节点
    while (tmp && tmp->next) {
        p = tmp;
        tmp = tmp->next;

        // 输出每次遍历节点的信息
        printf("Checking node with audio_buffer=%p\n", tmp->audio_buffer);

        if (tmp->audio_buffer == audio_buffer) {
            // 找到匹配的节点，进行删除操作
            printf("Found node to delete with audio_buffer=%p\n", audio_buffer);
            p->next = tmp->next; // 删除当前节点
            free(tmp); // 释放节点
            printf("Node deleted\n");
            return; // 删除后直接返回
        }
    }

    // 如果链表中没有找到匹配的节点
    printf("No matching node found to delete\n");
}

/*

*/


/*
函数功能:遍历链表，得到节点总数量
*/
int List_GetNodeCnt(struct AUDIO_DATA *head)
{
    // 检查头指针是否为空
    if (head == NULL) {
        printf("警告: 链表头指针为NULL\n");
        return 0; // 如果头指针为空，返回0
    }

    int cnt = 0;
    struct AUDIO_DATA *tmp = head;

    // 遍历链表，统计节点数
	while(tmp->next)
	{
		tmp=tmp->next;
		cnt++;  
          printf("链表节点总数: %d\n", cnt);
	}
	return cnt;


}


int capture_audio_data_init( char *audio_dev)
{
	int i;
	int err;
	
	buffer_frames = 1024;

	unsigned int rate = AUDIO_RATE_SET;// 常用的采样频率: 44100Hz 、16000HZ、8000HZ、48000HZ、22050HZ
	capture_handle;// 一个指向PCM设备的句柄
	snd_pcm_hw_params_t *hw_params; //此结构包含有关硬件的信息，可用于指定PCM流的配置
	
	/*注册信号捕获退出接口*/
	printf("进入main\n");
	/*PCM的采样格式在pcm.h文件里有定义*/
	format=SND_PCM_FORMAT_S16_LE; // 采样位数：16bit、LE格式
	
	/*打开音频采集卡硬件，并判断硬件是否打开成功，若打开失败则打印出错误提示*/
	if ((err = snd_pcm_open (&capture_handle, audio_dev,SND_PCM_STREAM_CAPTURE,0))<0) 
	{
		printf("无法打开音频设备: %s (%s)\n",  audio_dev,snd_strerror (err));
		exit(1);
	}
	printf("音频接口打开成功.\n");
	
 
	/*分配硬件参数结构对象，并判断是否分配成功*/
	if((err = snd_pcm_hw_params_malloc(&hw_params)) < 0) 
	{
		printf("无法分配硬件参数结构 (%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("硬件参数结构已分配成功.\n");
	
	/*按照默认设置对硬件对象进行设置，并判断是否设置成功*/
	if((err=snd_pcm_hw_params_any(capture_handle,hw_params)) < 0) 
	{
		printf("无法初始化硬件参数结构 (%s)\n", snd_strerror(err));
		exit(1);
	}
	printf("硬件参数结构初始化成功.\n");
 
	/*
		设置数据为交叉模式，并判断是否设置成功
		interleaved/non interleaved:交叉/非交叉模式。
		表示在多声道数据传输的过程中是采样交叉的模式还是非交叉的模式。
		对多声道数据，如果采样交叉模式，使用一块buffer即可，其中各声道的数据交叉传输；
		如果使用非交叉模式，需要为各声道分别分配一个buffer，各声道数据分别传输。
	*/
	if((err = snd_pcm_hw_params_set_access (capture_handle,hw_params,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) 
	{
		printf("无法设置访问类型(%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("访问类型设置成功.\n");
 
	/*设置数据编码格式，并判断是否设置成功*/
	if ((err=snd_pcm_hw_params_set_format(capture_handle, hw_params,format)) < 0) 
	{
		printf("无法设置格式 (%s)\n",snd_strerror(err));
		exit(1);
	}
	fprintf(stdout, "PCM数据格式设置成功.\n");
 
	/*设置采样频率，并判断是否设置成功*/
	if((err=snd_pcm_hw_params_set_rate_near (capture_handle,hw_params,&rate,0))<0) 
	{
		printf("无法设置采样率(%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("采样率设置成功\n");
 
	/*设置声道，并判断是否设置成功*/
	if((err = snd_pcm_hw_params_set_channels(capture_handle, hw_params,AUDIO_CHANNEL_SET)) < 0) 
	{
		printf("无法设置声道数(%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("声道数设置成功.\n");
 
	/*将配置写入驱动程序中，并判断是否配置成功*/
	if ((err=snd_pcm_hw_params (capture_handle,hw_params))<0) 
	{
		printf("无法向驱动程序设置参数(%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("参数设置成功.\n");
	/*使采集卡处于空闲状态*/
	snd_pcm_hw_params_free(hw_params);
 
	/*准备音频接口,并判断是否准备好*/
	if((err=snd_pcm_prepare(capture_handle))<0) 
	{
		printf("无法使用音频接口 (%s)\n",snd_strerror(err));
		exit(1);
	}
	printf("音频接口准备好.\n");
	
	return 0;
}

unsigned char audio_read_buff[2048];
//音频采集线程
int capture_audio_data(snd_pcm_t *capture_handle,int buffer_frames)
{
	int err;
	//因为frame样本数固定为1024,而双通道，每个采样点2byte，所以一次要发送1024*2*2byte数据给frame->data[0];
	/*配置一个数据缓冲区用来缓冲数据*/
	//snd_pcm_format_width(format) 获取样本格式对应的大小(单位是:bit)
	int frame_byte=snd_pcm_format_width(format)/8;

	/*开始采集音频pcm数据*/
	printf("开始采集数据...\n");
	int i;
	char *audio_buffer;
	while(1) 
	{
		audio_buffer=malloc(buffer_frames*frame_byte*AUDIO_CHANNEL_SET); //2048
		if(audio_buffer==NULL)
		{
			printf("缓冲区分配错误.\n");
			break;
		}
		
		/*从声卡设备读取一帧音频数据:2048字节*/
		if((err=snd_pcm_readi(capture_handle,audio_read_buff,buffer_frames))!=buffer_frames) 
		{
			  printf("从音频接口读取失败(%s)\n",snd_strerror(err));
			  exit(1);
		}
	
		pthread_mutex_lock(&mutex_audio); /*互斥锁上锁*/
		memcpy(audio_buffer,audio_read_buff,buffer_frames*frame_byte*AUDIO_CHANNEL_SET);
		//添加节点
		List_AddNode(list_head,audio_buffer);
		pthread_mutex_unlock(&mutex_audio); /*互斥锁解锁*/
	}
 
	/*释放数据缓冲区*/
	free(audio_buffer);
 
	/*关闭音频采集卡硬件*/
	snd_pcm_close(capture_handle);
 
	/*关闭文件流*/
	fclose(pcm_data_file);

	return 0;
}


